﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Weapp.Data;
using Weapp.Data.Entities;
using Weapp.Web.Models;

namespace Weapp.Web.Controllers
{
    public class UserDnDController : BaseApiController
    {
        public UserDnDController(IWeappRepository repo) : base(repo)
        {

        }

        public HttpResponseMessage Post([FromBody] UserDnDModel userDnDModel)
        {
            try
            {
                var entity = TheModelFactory.Parse(userDnDModel);

                if (entity == null) Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read size category from body");

                if (TheRepository.Insert(entity) && TheRepository.SaveAll())
                {
                    return Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(entity));
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to the database.");
                }
            }
            catch (Exception ex)
            {

                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }
    }
}